$(document).ready(function(){
	var currentPanel = 'whatsGoingOn';

	$('ul#mainTabSwitch h2 a.switch').click( function (e) {
		e.preventDefault();
		
		var target = $(this).attr('href');
		var others = $('ul#mainTabSwitch h2 a.switch').not(this);
		
		$(this).parents('li').addClass('current');
		$(others).parents('li').removeClass('current');
		
		$( others.attr('href') ).fadeOut( 'fast', function() {
			$(target).fadeIn('');
			currentPanel = unescape(target.substring(1));
		});
		
	});
	
	$('ul.calendar > li').click( function(e) {
		e.preventDefault();
		
		$(this).addClass('selected');
		
		if ( e.shiftKey && $(this).siblings('.selected').andSelf().length > 1 ) {
			var firstposition = 0;
			
			var items = $(this).parent().children('li');
			
			$(items).each( function(i) {
				if( $(this).hasClass('firstclick') ) {
					firstposition = i;
				}
			});
		
			position = $(this).parent().children('li').index(this);
			
			firstIndex = Math.min(firstposition, position);
			lastIndex = Math.max(firstposition, position);
			
			$(items).removeClass('selected');
			$(items).removeClass('selectionStart')
			$(items).removeClass('selectionEnd')
			
			for (var y = firstIndex; y <= lastIndex; y ++) {
				$( $(items).get(y) ).addClass('selected');
			}
			
			if ( firstIndex != lastIndex ) {
				$( $(items).get(firstIndex) ).addClass('selectionStart');
				$( $(items).get(lastIndex) ).addClass('selectionEnd');
			}
		
			$('#' +currentPanel + ' ul#theList').calendarData( $( $(items).get(firstIndex) ).find('abbr').attr('title'), $( $(items).get(lastIndex) ).find('abbr').attr('title'), currentPanel );
		
		} else {
			$(this).siblings().removeClass('selected');
			$(this).siblings().removeClass('firstclick');
			$(this).siblings().andSelf().removeClass('selectionStart');
			$(this).siblings().andSelf().removeClass('selectionEnd');
			
			$('#' + currentPanel + ' ul#theList').calendarData( $(this).find('abbr').attr('title'), $(this).find('abbr').attr('title'), currentPanel );
		}
		
		if ( $(this).siblings('.selected').length == 0 ) $(this).addClass('firstclick');
		
	});
	
	$('span.show a').click( function(e) {
		e.preventDefault();
		
		var linkclass = $(this).attr('class')
		
		if ( $(this).hasClass('all') ) $('#' + currentPanel + ' ul#theList li').fadeIn();
		else { 
			$('#' + currentPanel + ' ul#theList li div.meta span').not( '.'+linkclass ).parents('li').fadeOut();
			$('#' + currentPanel + ' ul#theList li div.meta span.'+linkclass ).parents('li').fadeIn();
		}
		
	});
		
	var hash = unescape(self.document.location.hash.substring(1));
	
	if (hash.length) $('ul#mainTabSwitch h2 a.switch[href="#'+hash+'"]').click();
	else $('ul#mainTabSwitch h2 a.switch:first').click();

	

});

$.fn.calendarData = function (from, to, method) {
	var targetContainer = this;
	
	$.post( siteURL + '/epicentr/'+method,
			{startdate: from,
			enddate: to },
			function (updatehtml) {
				if (updatehtml != 0) $(targetContainer).html(updatehtml);
				else $(targetContainer).html('<p>Nothing happened on the dates selected.</p>');
				
				$("a.complete").markAs('complete');
				$("a.notcomplete").markAs('notcomplete');
				$("a.dismiss").markAs('dismiss');
				$("a.recall").markAs('recall');
			}
		);
}